package com.lyt.springboot.designPattern.chainResponsibilityPattern;

// SupportHandler.java
public class SupportHandler implements Handler {
    private int level;
    private Handler nextHandler;

    public SupportHandler(int level) {
        this.level = level;
    }

    @Override
    public void handleRequest(String request) {
        if (canHandle(request)) {
            System.out.println("Handler at level " + level + " handled the request: " + request);
        } else {
            if (nextHandler != null) {
                System.out.println("Handler at level " + level + " cannot handle the request. Passing to next handler.");
                nextHandler.handleRequest(request);
            } else {
                System.out.println("No handler could process the request: " + request);
            }
        }
    }

    @Override
    public void setNextHandler(Handler handler) {
        this.nextHandler = handler;
    }

    private boolean canHandle(String request) {
        // 这里可以添加更复杂的逻辑来决定是否处理请求
        // 例如，根据请求的内容或类型来决定
        // 在这个例子中，我们简单地根据请求的开头字符来决定
        char firstChar = request.charAt(0);
        return (level == 1 && firstChar == 'A') ||
                (level == 2 && firstChar == 'B') ||
                (level == 3 && firstChar == 'C');
    }
}